package demo.collections;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

@SuppressWarnings("unused")
public class MapTT {
	public static void main(String[] args) {
		String str = "sdbguiydsbgiundsoifngbiudhtgibfndxiuydlitfunbiufxdngbizpmundiufbnidufsbntioudrstnfioundrtiubghoudrbngiudfrtnhgointdfouibnistdfubhgnoiundsfiuobhgiudrsbfgiudrsfbgiudntfsobgindiutfngioubhnderoiufgnodifngibufdjhdfjhbvuidfngiudfrtgrgerdgdsgdrgbudybgvdubvgdshbfkugshdiufgskjdbfksbcvyiusgtksdgbfiubsrdjkfvbsdjfgkdjsgbfkjdsgbfiujydsbcvuysdgfjhdsybvuydsgfjkusdgbjfygdsifuygisdyugfvisaudbfgkasdgfikusgdfiusbgdfvcjhsdgfisudgfkdsubfirugbkjsdbgiursbiguberuyifbgijkdfbgiusbdikujbfosbibqwqwofnuisdbfkjniuserbgiubsdiuygbesiuybg";
		method2(str);
		method3(str);
	}

	private static void method3(String str) {
		long sta = System.currentTimeMillis();
		HashMap<Character, Integer> map = new HashMap<Character, Integer>();
		char[] arr = str.toCharArray();
		for (char c : arr) {
			if (map.containsKey(c))
				map.put(c, map.get(c) + 1);
			else
				map.put(c, 1);
		}
		System.out.println(map);
		System.out.println(System.currentTimeMillis() - sta);
	}

	private static void method2(String str) {
		long sta = System.currentTimeMillis();
		HashMap<Character, Integer> map = new HashMap<Character, Integer>();
		char[] arr = str.toCharArray();
		Integer num = null;
		for (char c : arr) {
			num = map.get(c);
			map.put(c, num == null ? 1 : num + 1);
		}
		System.out.println(map);
		System.out.println(System.currentTimeMillis() - sta);
	}

	// HashMap���ֱ���
	private static void method1() {
		HashMap<String, Integer> hm = new HashMap<String, Integer>();
		hm.put("С��", 5);
		hm.put("С��", 6);
		hm.put("Сǿ", 7);
		for (String key : hm.keySet()) {
			System.out.println(key + "=" + hm.get(key));
		}
		System.out.println("----------");
		for (Entry<String, Integer> entry : hm.entrySet()) {
			System.out.println(entry.getKey() + "=" + entry.getValue());
		}
	}
}
